package cn.iocoder.yudao.module.erp.service.business.produce.producework;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.producework.vo.ProduceWorkPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.producework.vo.ProduceWorkSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.produce.producework.ProduceWorkDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.produce.producework.ProduceWorkMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PRODUCE_WORK_NOT_EXISTS;

/**
 * 工作中心 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class ProduceWorkServiceImpl implements ProduceWorkService {

    @Resource
    private ProduceWorkMapper produceWorkMapper;

    @Override
    public Long createProduceWork(ProduceWorkSaveReqVO createReqVO) {
        // 插入
        ProduceWorkDO produceWork = BeanUtils.toBean(createReqVO, ProduceWorkDO.class);
        produceWorkMapper.insert(produceWork);
        // 返回
        return produceWork.getId();
    }

    @Override
    public void updateProduceWork(ProduceWorkSaveReqVO updateReqVO) {
        // 校验存在
        validateProduceWorkExists(updateReqVO.getId());
        // 更新
        ProduceWorkDO updateObj = BeanUtils.toBean(updateReqVO, ProduceWorkDO.class);
        produceWorkMapper.updateById(updateObj);
    }

    @Override
    public void deleteProduceWork(Long id) {
        // 校验存在
        validateProduceWorkExists(id);
        // 删除
        produceWorkMapper.deleteById(id);
    }

    private void validateProduceWorkExists(Long id) {
        if (produceWorkMapper.selectById(id) == null) {
            throw exception(PRODUCE_WORK_NOT_EXISTS);
        }
    }

    @Override
    public ProduceWorkDO getProduceWork(Long id) {
        return produceWorkMapper.selectById(id);
    }

    @Override
    public PageResult<ProduceWorkDO> getProduceWorkPage(ProduceWorkPageReqVO pageReqVO) {
        return produceWorkMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProduceWorkDO> getProduceWorkList() {
        return produceWorkMapper.selectList();
    }

}